#include "authdialog.h"
#include "ui_authdialog.h"
#include "settings.h"
#include "Interfaces/tray.h"

AuthDialog::AuthDialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::AuthDialog)
{
    ui->setupUi(this);
    this->setFixedSize(this->size());
    auth_url=QUrl("http://shotscreens.appspot.com/account/login/");
    manager=new QNetworkAccessManager();
    connect(manager, SIGNAL(finished(QNetworkReply*)), SLOT(onFinished(QNetworkReply*)));
    connect(ui->exitButton, SIGNAL(clicked()), SLOT(onExitButtonClicked()));
    connect(ui->okButton, SIGNAL(clicked()), SLOT(onOkButtonClicked()));
    connect(ui->registrationButton, SIGNAL(clicked()), SLOT(onRegistrationButtonClicked()));
    login.clear();
    password.clear();
    is_saved=false;
}

AuthDialog::~AuthDialog()
{
    delete ui;
}

void AuthDialog::onExitButtonClicked()
{
    QApplication::quit();
}

void AuthDialog::onOkButtonClicked()
{
    req=QNetworkRequest(auth_url);
    QByteArray params;
    params.clear();
    params.append("username=");
    params.append(ui->userNameEdit->text());
    params.append("&password=");
    params.append(ui->passwordEdit->text());

    is_saved=ui->checkBox->checkState();
    ui->userNameEdit->setEnabled(false);
    ui->passwordEdit->setEnabled(false);
    ui->exitButton->setEnabled(false);
    ui->okButton->setEnabled(false);
    ui->checkBox->setEnabled(false);
    manager->post(req,params);
}

void AuthDialog::onFinished(QNetworkReply *reply)
{
    QByteArray data;
    data=reply->readAll();
    if(reply->error())
    {
        qDebug("Error: %s", reply->errorString().toAscii().data());
        return;
    }

    if(reply->operation()==QNetworkAccessManager::PostOperation)
    {
       if(data.length() == 0)
       {
           if(is_saved==true)
           {
               Settings::instance().setLogin(ui->userNameEdit->text());
               Settings::instance().setPassword(ui->passwordEdit->text());

           }
           this->hide();
           emit success();
       }
       else
       {
           MessageBox.critical(this,"Sign - in error","Incorrect user name or password!", QMessageBox::Ok,QMessageBox::NoButton);
           Settings::instance().setLogin("");
           Settings::instance().setPassword("");
           ui->userNameEdit->setText("");
           ui->passwordEdit->setText("");
           ui->userNameEdit->setEnabled(true);
           ui->passwordEdit->setEnabled(true);
           ui->exitButton->setEnabled(true);
           ui->okButton->setEnabled(true);
           ui->checkBox->setEnabled(true);
           login.clear();
           password.clear();
           is_saved=false;
           if(this->isHidden())
           {
               this->show();
           }
       }
    }
    reply->deleteLater();
}

//void AuthDialog::SetTrayHandle(ShotScreens *tray)
//{
//    tray_handle = tray;
//    tray_handle->showTray();
//}

bool AuthDialog::OnLoad()
{
    login=Settings::instance().getLogin();
    password=Settings::instance().getPassword();

    if((login.length() == 0) || (password.length() == 0))
    {
        return true;
    }

    req=QNetworkRequest(auth_url);
    QByteArray params;
    params.clear();
    params.append("username=");
    params.append(login);
    params.append("&password=");
    params.append(password);
    ui->userNameEdit->setText(login);
    ui->passwordEdit->setText(password);
    ui->userNameEdit->setEnabled(false);
    ui->passwordEdit->setEnabled(false);
    ui->exitButton->setEnabled(false);
    ui->okButton->setEnabled(false);
    ui->checkBox->setEnabled(false);
    is_saved=true;
    manager->post(req,params);
    return false;
}


void AuthDialog::onRegistrationButtonClicked()
{
    RegDialog.ConstructGetChallengeQuery();
    RegDialog.OnLoad();

    RegDialog.setModal(true);
    RegDialog.showNormal();
}

void AuthDialog::closeEvent(QCloseEvent *e)
{
    QApplication::quit();
    e->accept();
}
